<?php
//获取用户列表
function user_list(){//exit('###');
	set_time_limit(0);
	$obj = M('Token')->limit(1)->order('create_time desc')->find();
	$access_token=$obj['value'];
	$next_openid='';
	$url='https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$access_token.'&next_openid='.$next_openid.'';
	$res = file_get_contents($url); 	
	$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
	$openid_list=$result['data']['openid'];
	foreach ($openid_list as $key=>$value) {
		$openid=$value;
		$where['openid']=$openid;
		$res_select=M('user')->where($where)->find();
		if (empty($res_select)) {
		$info_url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
		$info_res = file_get_contents($info_url); //获取文件内容或获取网络请求的内容
		$info_result = json_decode($info_res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
		//查询结果，如果有则不进行插入
		
		//添加数据
		$data = array('subscribe' =>$info_result['subscribe'] ,
					  'openid' =>$info_result['openid'] ,
					  'nickname' =>$info_result['nickname'] ,
					  'sex' => $info_result['sex'],
					  'language' =>$info_result['language'] ,
					  'city' =>$info_result['city'] ,
					  'province' =>$info_result['province'] ,
					  'country' =>$info_result['country'] ,
					  'headimgurl' =>$info_result['headimgurl'] ,
					  'subscribe_time' =>date('Y-m-d H:i:s',$info_result['subscribe_time']),
					  'remark' =>$info_result['remark'] ,
					  'groupid' =>$info_result['groupid'] ,
					  'tagid_list' =>$info_result['tagid_list'] ,
					 
					  'ctime'=>time(),
						 );
		$res_add=M('user')->data($data)->add();
	 }
	}
}
function createMenu($data,$access_token){
	//	print_r($access_token);print_r($data);exit;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$tmpInfo = curl_exec($ch);
	if (curl_errno($ch)) {
	  return curl_error($ch);
	}
	curl_close($ch);
	
	$new_tmpInfo=json_decode($tmpInfo,true);
	$errcode=$new_tmpInfo['errcode'];
	$errmsg=$new_tmpInfo['errmsg'];
	if ($errcode == 0 && $errmsg=='ok') {
		echo '微信菜单生成成功';
	}else{
		echo '微信菜单生成失败';
	}
	return $tmpInfo;
}
	/**
	 * 错误跳转
	 */
	function error($msg,$url){
		header("Content-type:textml;charset=utf-8");
		echo "<script>alert('$msg');window.location.href='$url';</script>";exit;
	}

	//生成优惠券方法
	function guid(){
		static $guid = '';
        $uid = uniqid("", true);
        $data = $namespace;
        $data .= $_SERVER['REQUEST_TIME'];			//得到请求开始时的时间戳
        $data .= $_SERVER['HTTP_USER_AGENT'];		//判断用户是否是手机
        $data .= $_SERVER['LOCAL_ADDR'];			//获取服务器端IP地址
        $data .= $_SERVER['LOCAL_PORT'];			//本机端口
        $data .= $_SERVER['REMOTE_ADDR'];			//客户端ip
        $data .= $_SERVER['REMOTE_PORT'];			//获取源端口号的函数 
        $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
        // test str combination
        $guid =
        substr($hash, 0, 6) .
        '-' .
        substr($hash, 8, 4) .
        '-' .
        substr($hash, 12, 4) .
        '-' .
        substr($hash, 16, 4) .
        '-' .
        substr($hash, 20, 8);
      	return $guid;
    }
?>